// function to create an XMLHttpClient in a cross-browser manner
function XMLRequest()
 {
    var xmlhttp;
    try
    {
        // Mozilla / Safari / IE7
        xmlhttp = new XMLHttpRequest();
    } catch(e)
    {
        // IE
        var XMLHTTP_IDS = new Array('MSXML2.XMLHTTP.5.0',
        'MSXML2.XMLHTTP.4.0',
        'MSXML2.XMLHTTP.3.0',
        'MSXML2.XMLHTTP',
        'Microsoft.XMLHTTP');
        var success = false;
        for (var i = 0; i < XMLHTTP_IDS.length && !success; i++)
        {
            try
            {
                xmlhttp = new ActiveXObject(XMLHTTP_IDS[i]);
                success = true;
            } catch(e)
            {
                }
        }
        if (!success)
        {
            throw new Error('Unable to create XMLHttpRequest.');
        }
    }
    return xmlhttp;
}

function _DisplayTips(cDiv, cCaption)
 {
    window.status = cCaption;

    var overlayer = document.getElementById(cDiv);
    if (overlayer)
    {
        overlayer.style.display = 'block';
    }

    overlayer = document.getElementById("messagelayer");
    if (overlayer)
    {
        overlayer.style.display = 'block';

        var message_caption = document.getElementById("message_caption");
        message_caption.innerHTML = cCaption;
    }
}

function AsyncRequest()
 {
    this.XRequestPage = "";
    this.Mode = "POST";
    this.Tips = false;
    this.ProgressMode = 0;
    this.XMLHttp = new XMLRequest();

    this.setPage = function(xPage)
    {
        this.XRequestPage = xPage;
    }
    this.setMode = function(xMode)
    {
        this.Mode = xMode;
    }
    this.AbortCallBack = function()
    {
        if (this.XMLHttp)
        {
            this.XMLHttp.abort();
        }
    }

    this.OnLoading = function()
    {
        // Loading
        }

    this.OnLoaded = function()
    {
        // Loaded
        }

    this.OnInteractive = function()
    {
        // Interactive
        }

    this.OnComplete = function(responseText, responseXml)
    {
        // Complete
        }

    this.OnAbort = function()
    {
        // Abort
        }

    this.OnError = function(status, statusText)
    {
        // Error
        }

    this.Progress = function(cCaption)
    {
        if (this.Tips)
        {
            _DisplayTips("overlayer", cCaption);
        }
    }

    this.ReadyStateChange = function()
    {
        var temp = new Date()
        if (this.XMLHttp.readyState == 1)
        {
            this.Progress("overlayer", lbl_Connecting);
            this.OnLoading();
        }
        else if (this.XMLHttp.readyState == 2)
        {
            this.Progress("overlayer", lbl_Sending);
            this.OnLoaded();
        }
        else if (this.XMLHttp.readyState == 3)
        {
            this.Progress("overlayer", lbl_Waiting);
            this.OnInteractive();
        }
        else if (this.XMLHttp.readyState == 4)
        {
            if (this.XMLHttp.status == 0)
            {
                this.Progress("overlayer", lbl_OnAbort);
                this.OnAbort();
            } else if (this.XMLHttp.status == 200 && this.XMLHttp.statusText == "OK")
            {

                this.Progress("overlayer", lbl_Processing);

                this.OnComplete(this.XMLHttp.responseText, this.XMLHttp.responseXML);

                if (this.Tips)
                {
                    setTimeout("_HideTips('overlayer')", 100);
                }

            } else {
                if (this.Tips)
                {
                    setTimeout("_HideTips('overlayer')", 100);
                    setTimeout("_DisplayTips('messagelayer','" + lbl_Connect_Failure + "');", 200);

                }
                this.OnError(this.XMLHttp.status, this.XMLHttp.statusText, this.XMLHttp.responseText);
            }
        }
    }

    this.DoCallBack = function(cData, eventArgument)
    {
        var theform = document.forms[0];
        var thePage = this.XRequestPage;
        var eName = '';
        var eType = '';

        var theData = 'IsCallBack=true&AjaxCallback=true&';


        if (this.Tips)
        {
            _HideTips('messagelayer');
            _DisplayTips("overlayer", lbl_Connecting);
        }

        if (cData != "")
        {
            theData += cData + "&";
        }

        if (this.Mode == "POST")
        {
            for (var i = 0; i < theform.elements.length; i++)
            {
                eName = theform.elements[i].name;
                eType = theform.elements[i].type.toUpperCase();

                if (eType == "TEXT" || eType == "TEXTAREA" || eType == "HIDDEN" || eType == "SELECT-ONE")
                {
                    if (eName && eName != '')
                    {
                        if (eName == '__EVENTTARGET' || eName == '__EVENTARGUMENT' || eName == '__VIEWSTATE' || eName == 'ZZFrmValidate' || (eName == 'ZZUID_STATE' && eventArgument != 'Ajax'))
                        {
                            // Do Nothing
                            }
                        else if (eName.substr(eName.length - 5, 5) == '_data')
                        {
                            }
                        else if (eName.substr(eName.length - 7, 7) == '_XMLDAT')
                        {

                            }
                        else
                        {
                            theData = theData + escape(eName.split("$").join(":")) + '=' + URLEncode(theform.elements[i].value);
                            if (i != theform.elements.length - 1)
                            {
                                theData = theData + '&';
                            }
                        }
                    }
                }
            }
        }

        if (theData.indexOf("&d=") == -1 && thePage.indexOf("d=") == -1)
        {
            if (thePage.indexOf("?") == -1)
            {
                thePage = thePage + "?d={lnk_dbName}";
            } else {
                thePage = thePage + "&d={lnk_dbName}";
            }
        }

        if (theData.indexOf("t=") == -1 && thePage.indexOf("t=") == -1)
        {
            if (thePage.indexOf("?") == -1)
            {
                thePage = thePage + "?t={ZZLiveBlock}";
            } else {
                thePage = thePage + "&t={ZZLiveBlock}";
            }
        }

        //window.open(thePage);
        if (this.XMLHttp)
        {
            if (this.XMLHttp.readyState == 4 || this.XMLHttp.readyState == 0)
            {
                if (this.Mode == "POST")
                {
                    this.XMLHttp.open('POST', thePage, true);
                } else {
                    this.XMLHttp.open('GET', thePage, true);
                }

                var oParent = this;

                this.XMLHttp.onreadystatechange = function()
                {
                    if (oParent)
                    {
                        oParent.ReadyStateChange();
                        if (oParent.XMLHttp.readyState == 4)
                        {
                            oParent = null;
                            if (this.Tips)
                            {
                                setTimeout("_HideTips('overlayer')", 100);
                            }
                        }
                    }
                };

                this.XMLHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                if (this.Mode == "POST")
                {
                    this.XMLHttp.send(theData);
                } else {
                    this.XMLHttp.send(null);
                }
            }
        }
    }
}
